package com.dfec.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.cache.redis.UrlDataCash;
import com.dfec.model.Msg;

public class ResultHandler {
	/**
	 * 
	 * @param request
	 * @param response 
	 * @param getdata  执行结果
	 * @param useCash  是否缓存本次请求数据
	 */
	public  static void handler(HttpServletRequest request, HttpServletResponse response,List<Object> getdata,boolean useCash) {
		 List<Object> list=null;
		 Msg msg=null; 
		 list = new ArrayList<>();
		  msg = new Msg();
  	try {
      		
      		if(!getdata.get(0).toString().equals("[]")){
      			msg.setCode(Consists.DATA_Code_OK);
      			msg.setMsg(Consists.DATA_Msg_OK);
      		}else {
      			msg.setCode(Consists.DATA_Code_OK__NODATA);
      			msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
      		}
      		     list.add(msg);
        		 list.add(getdata.get(0));
        		 //系统配置了缓存并且需要缓存
        		 if(PropertiesUtil.config_redis_use&&useCash) {
        			 UrlDataCash.saveUrlData(UrlDataCash.urlKey(request).toString(), JSON.toJSONString(list));	
				 }
        		ResponseUtil.sendScript(response, JSON.toJSONString(list));
      		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg.setCode(Consists.Error_Code_ServerError);
				msg.setMsg(Consists.Error_Msg_ServerError);
				list.add(msg);
				list.add(new ArrayList<>());
				ResponseUtil.sendScript(response, JSON.toJSONString(list));
			}
   }
}
